<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单流水" prop="orderFlow">
        <el-input
          v-model="queryParams.orderFlow"
          placeholder="请输入订单流水"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="套餐名称" prop="packageName">
        <el-input
          v-model="queryParams.packageName"
          placeholder="请输入套餐名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="支付时间">
        <el-date-picker
          v-model="daterangePaymentTime"
          style="width: 240px"
          value-format="yyyy-MM-dd "
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.svc_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="退款时间">
        <el-date-picker
          v-model="daterangeRefundTime"
          style="width: 240px"
          value-format="yyyy-MM-dd "
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="销售" prop="salesperson">
        <el-input
          v-model="queryParams.salesperson"
          placeholder="请输入销售"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="渠道" prop="channel">
        <el-select v-model="queryParams.channel" placeholder="请选择渠道" clearable>
          <el-option
            v-for="dict in dict.type.svc_qd_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:orderinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:orderinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:orderinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:orderinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" width="50" align="center" prop="ckOrderInfoId" />
      <el-table-column label="订单流水" width="100" align="center" prop="orderFlow" />
      <el-table-column label="openid" width="120" align="center" prop="openid" />
      <!--<el-table-column label="客户ID" align="center" prop="ckCustomerInfoId" />-->
      <el-table-column label="客户名称" width="100" align="center" prop="customerName" />
     <!-- <el-table-column label="套餐ID" align="center" prop="ckPackageInfoId" />-->
      <el-table-column label="套餐名称" width="100" align="center" prop="packageName" />

      <el-table-column label="付款金额" width="100" align="center" prop="payment" />
      <el-table-column label="商品规格" align="center" prop="specification" />
      <el-table-column label="购买数量" align="center" prop="paycount" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.svc_order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>

      <el-table-column label="支付方式" width="100" align="center" prop="paymentMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_pay_type" :value="scope.row.paymentMethod"/>
        </template>
      </el-table-column>

      <!--<el-table-column label="销售" align="center" prop="salesperson" />

-->



      <el-table-column label="渠道" align="center" prop="channel" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.svc_qd_name" :value="scope.row.channel"/>
        </template>
      </el-table-column>

      <el-table-column label="支付时间" align="center" prop="paymentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentTime)}}</span>
        </template>
      </el-table-column>

      <el-table-column label="退款金额" align="center" prop="refundAmount" />

      <el-table-column label="退款原因" align="center" prop="refundReason" />

      <el-table-column label="退款时间" align="center" prop="refundTime" width="120">
        <template slot-scope="scope">
          <span>{{parseTime(scope.row.refundTime) }}</span>
        </template>
      </el-table-column>
     <!-- <el-table-column label="备注" align="center" prop="notes" />-->


      <el-table-column label="操作" width="160" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:orderinfo:edit']"
          >退款处理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:orderinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单流水" prop="orderFlow">
          <el-input v-model="form.orderFlow" placeholder="请输入订单流水" />
        </el-form-item>

        <el-form-item label="openid" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入openid" />
        </el-form-item>

        <!--<el-form-item label="客户ID" prop="ckCustomerInfoId">
          <el-input v-model="form.ckCustomerInfoId" placeholder="请输入客户ID" />
        </el-form-item>-->

        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>

        <el-form-item label="套餐ID" prop="ckPackageInfoId">
          <el-input v-model="form.ckPackageInfoId" placeholder="请输入套餐ID" />
        </el-form-item>

        <el-form-item label="套餐名称" prop="packageName">
          <el-input v-model="form.packageName" placeholder="请输入套餐名称" />
        </el-form-item>

        <el-table-column label="商品规格" align="center" prop="specification" />
        <el-table-column label="购买数量" align="center" prop="paycount" />

        <el-form-item label="销售" prop="salesperson">
          <el-input v-model="form.salesperson" placeholder="请输入销售" />
        </el-form-item>
        <el-form-item label="渠道" prop="channel">
          <el-select v-model="form.channel" placeholder="请选择渠道">
            <el-option
              v-for="dict in dict.type.svc_qd_name"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="付款金额" prop="payment">
          <el-input v-model="form.payment" placeholder="请输入付款金额" />
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentMethod">
          <el-select v-model="form.paymentMethod" placeholder="请选择支付方式">
            <el-option
              v-for="dict in dict.type.sys_pay_type"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付时间" prop="paymentTime">
          <el-date-picker clearable
            v-model="form.paymentTime"
            type="date"
            value-format="yyyy-MM-dd hh:mm:ss"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-select v-model="form.orderStatus" placeholder="请选择订单状态">
            <el-option
              v-for="dict in dict.type.svc_order_status"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount">
          <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="退款原因" prop="refundReason">
          <el-input v-model="form.refundReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="退款时间" prop="refundTime">
          <el-date-picker clearable
            v-model="form.refundTime"
            type="date"
            value-format="yyyy-MM-dd hh:mm:ss"
            placeholder="请选择退款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrderinfo, getOrderinfo, delOrderinfo, addOrderinfo, updateOrderinfo } from "@/api/system/orderinfo";

export default {
  name: "Orderinfo",
  dicts: ['svc_order_status', 'svc_qd_name', 'sys_pay_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 渠道时间范围
      daterangePaymentTime: [],
      // 渠道时间范围
      daterangeRefundTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderFlow: null,
        openid: null,
        /*ckCustomerInfoId: null,*/
        ckPackageInfoId: null,
        payment: null,
        paymentMethod: null,
        paymentTime: null,
        orderStatus: null,
        refundAmount: null,
        refundReason: null,
        refundTime: null,
        notes: null,
        salesperson: null,
        specification: null,
        paycount: null,
        channel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderFlow: [
          { required: true, message: "订单流水不能为空", trigger: "blur" }
        ],
        openid: [
          { required: true, message: "openid", trigger: "blur" }
        ],
        ckPackageInfoId: [
          { required: true, message: "套餐ID不能为空", trigger: "blur" }
        ],
        payment: [
          { required: true, message: "付款金额不能为空", trigger: "blur" }
        ],
        paymentMethod: [
          { required: true, message: "支付方式不能为空", trigger: "change" }
        ],
        paymentTime: [
          { required: true, message: "支付时间 yyyy-MM-dd hh:mm:ss不能为空", trigger: "blur" }
        ],
        orderStatus: [
          { required: true, message: "订单状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangePaymentTime && '' != this.daterangePaymentTime) {
        this.queryParams.params["beginPaymentTime"] = this.daterangePaymentTime[0];
        this.queryParams.params["endPaymentTime"] = this.daterangePaymentTime[1];
      }
      if (null != this.daterangeRefundTime && '' != this.daterangeRefundTime) {
        this.queryParams.params["beginRefundTime"] = this.daterangeRefundTime[0];
        this.queryParams.params["endRefundTime"] = this.daterangeRefundTime[1];
      }
      listOrderinfo(this.queryParams).then(response => {
        this.orderinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ckOrderInfoId: null,
        orderFlow: null,
        openid: null,
        /*ckCustomerInfoId: null,*/
        ckPackageInfoId: null,
        payment: null,
        paymentMethod: null,
        paymentTime: null,
        orderStatus: null,
        refundAmount: null,
        refundReason: null,
        refundTime: null,
        notes: null,
        specification: null,
        paycount: null,
        salesperson: null,
        channel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePaymentTime = [];
      this.daterangeRefundTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ckOrderInfoId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ckOrderInfoId = row.ckOrderInfoId || this.ids
      getOrderinfo(ckOrderInfoId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "退款处理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ckOrderInfoId != null) {
            var payments=this.form.payment;
            var refundAmounts=this.form.refundAmount;
            if(refundAmounts > payments ){
              this.$modal.msgSuccess("退款金额不能大于付款金额");
              //this.$modal.msgSuccess("退款处理成功");
            }else{
              updateOrderinfo(this.form).then(response => {
                this.$modal.msgSuccess("退款处理成功");
                this.open = false;
                this.getList();
              });
            }

          } else {
            addOrderinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ckOrderInfoIds = row.ckOrderInfoId || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ckOrderInfoIds + '"的数据项？').then(function() {
        return delOrderinfo(ckOrderInfoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/orderinfo/export', {
        ...this.queryParams
      }, `orderinfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
